function observer(target) { 
  if(typeof target !=='object') return target
  for(let key in target) {
    // 重新定义key
    defineReactive(target, key, target[key])
  }
}

function defineReactive(target, key, value) {
  observer(value)
  Object.defineProperty(target, key, {
    get() {
      // 收集依赖
      return value
    },
    set(newVal) {
      if(newVal !== value) {
        // 触发更新
        update()
      }
      
    }
  })
}

let methodsArray = ['pop', 'push', 'unshift', 'splice', 'shift', 'reverse','sort']
let originArray = Array.prototype
let newArrayMethod = Object.create(originArray)
methodsArray.forEach(method => {
  Object.defineProperty(newArrayMethod, method, {
    get() {
      // 通知更新
      // notify()
      return originArray[method].apply(this,arguments)
    }
  })
  defFunction(newArrayMethod, method, function mutator (...args) {
    const result = originArray[method].apply(this,args)
    //通知更新
    // notify()
    return result 
  })
})


//以下为vue源码 的实现
function defFunction (obj, key, val) {
  Object.defineProperty(obj, key, {
    value: val
  })
}



const arrayProto = Array.prototype
export const arrayMethods = Object.create(arrayProto)

const methodsArray = [
  'push',
  'pop',
  'shift',
  'unshift',
  'splice',
  'sort',
  'reverse'
]

/**
 * Intercept mutating methods and emit events
 */
 methodsArray.forEach(function (method) {
  // cache original method
  const original = arrayProto[method]
  def(arrayMethods, method, function mutator (...args) {
    const result = original.apply(this, args)
    const ob = this.__ob__
    let inserted
    switch (method) {
      case 'push':
      case 'unshift':
        inserted = args
        break
      case 'splice':
        inserted = args.slice(2) // ??? 没看明白 ，
        /** 21.05.06备注：args是调用方法时的所有参数组成的数组，
        第一个参数是startIndex,
        第二个是deleteCount,第三个开始是插入的数据，
        inserted = args.slice(2) , inserted 的值就是调用方法要插入的值要通过ob.observeArray 转换成响应式数据
        */
        break
    }
    if (inserted) ob.observeArray(inserted)
    // notify change
    ob.dep.notify()
    return result
  })
})